package com.coollang.smartbra.http;

import android.content.Context;
import android.widget.Toast;

import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.utils.MD5Utils;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.Urls;
import com.lzy.okgo.OkGo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Timmy
 * @Data: 2017/7/6
 * @Fuction:
 */

public class MyHttp {

    /**
     * 200 => 成功
     *    -1000 => 签名验证失败
     *    -1001 => 短信验证码不正确
     *    -1002 => 缺少参数
     *    -1003 => 参数错误
     *    -1004 => 获取验证码失败
     *    -10001 => 手机已注册
     *    -10002 => 手机格式错误
     *    -10003 => 注册失败
     *    -10004 => 用户不存在
     *    -10005 => 用户名或密码错
     *    -10006 => 用户异常
     *    -10007 => 密码格式不正确
     *    -10008 => 修改用户信息失败
     *    -20001 => 动作不存在
     *    -20002 => 用户计划不存在
     *    -20003 => 计划不存在
     *    -20004 => 删除动作失败
     *    -20005 => 动作列表为空
     *    -30001 => 该用户未绑定设备
     *    -30002 => 绑定信息不存在
     *    -30003 => 解除绑定失败
     *    -30004 => 删除设备失败
     *    -30005 => 绑定设备失败
     *    -40001 => 动作列表为空
     *    -50001 => 添加版本信息失败
     *    -50002 => 版本信息不存在
     */

    private static MyHttp instance;

    private MyHttp() {

    }

    public static MyHttp getInstance() {
        if (instance == null) {
            synchronized (MyHttp.class) {
                if (instance == null) {
                    instance = new MyHttp();
                }
            }
        }
        return instance;
    }

    // ====================================== 网络请求 =========================

    /**
     * 获取更新信息
     */
    public void getUpdateInfo(HttpCallBack calllBack) {
        OkGo.get(Urls.URL_UPDATE)
                .params(getCheckParams(Urls.PARAMS_GET_VERSION), false)
                .execute(calllBack);
    }

    /**
     * 登录
     */
    public void login(Map<String, String> paramsMap, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_LOGIN)
                .params(getCheckParams(Urls.PARAMS_LOGIN), false)
                .params(paramsMap, false)
                .execute(calllBack);
    }

    private Map<String, String> getCheckParams(String check) {
        String timeStamp = TimeUtils.getCurrentTimeUnixString();
        String sign = MD5Utils.getMD5(check + timeStamp);

        Map<String, String> map = new HashMap<>(16);
        map.put(Urls.PARAMS_TIMESTAMP, timeStamp);
        map.put(Urls.PARAMS_SIGN, sign);

        return map;
    }

    /**
     * 判断用户是否注册
     */
    public void checkUserIsRegister(String username, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_ISREGISTER)
                .params(getCheckParams(Urls.PARAMS_ISREGISTER), false)
                .params(Urls.USERNAME, username)
                .execute(calllBack);
    }

    /**
     * 获取手机验证码
     */
    public void getCode(String username, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_GETCODE)
                .params(getCheckParams(Urls.PARAMS_GETCODE), false)
                .params(Urls.USERNAME, username)
                .execute(calllBack);
    }

    /**
     * 注册账号
     */
    public void register(Map<String, String> paramsMap, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_REGISTER)
                .params(getCheckParams(Urls.PARAMS_REGISTER), false)
                .params(paramsMap, false)
                .execute(calllBack);
    }

    /**
     * 修改密码
     */
    public void changePassword(Map<String, String> paramsMap, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_CHANGEPASSWORD)
                .params(getCheckParams(Urls.PARAMS_CHANGEPASSWORD), false)
                .params(paramsMap, false)
                .execute(calllBack);
    }

    /**
     * 查看设备是否被绑定
     */
    public void isBinding(Map<String, String> paramsMap, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_IS_BINDING)
                .params(getCheckParams(Urls.PARAMS_IS_BINDING), false)
                .params(paramsMap, false)
                .execute(calllBack);
    }

    /**
     * 绑定设备
     */
    public void bindingDevice(Map<String, String> paramsMap, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_BINDING_DEVICE)
                .params(getCheckParams(Urls.PARAMS_BINDING_DEVICE), false)
                .params(paramsMap, false)
                .execute(calllBack);
    }

    /**
     * 解除绑定设备
     */
    public void unbindingDevice(Map<String, String> paramsMap, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_UNBINDING_DEVICE)
                .params(getCheckParams(Urls.PARAMS_UNBINDING_DEVICE), false)
                .params(paramsMap, false)
                .execute(calllBack);
    }

    /**
     * 上传跑步数据
     */
    public void postRunningData(Map<String, String> paramsMap, HttpCallBack calllBack) {
        OkGo.get(Urls.URL_ACTION_RUN)
                .params(getCheckParams(Urls.PARAMS_ADD_DISTANCE_ACTION), false)
                .params(paramsMap, false)
                .execute(calllBack);
    }

    /**
     * 获取U币数据详情
     */
    public void getCoinRecord(HttpCallBack calllBack) {
        OkGo.get(Urls.URL_UMONEY_DETAIL)
                .params(getCheckParams(Urls.PARAMS_GET_COIN_RECORD), false)
                .params(Urls.USERNAME, MyApplication.mLogInDataBean.getUsername())
                .execute(calllBack);
    }

    /**
     * 获取所有运动的总数
     */
    public void getActionsAmount(HttpCallBack calllBack) {
        OkGo.get(Urls.URL_ACTION_AMOUNT)
                .params(getCheckParams(Urls.PARAMS_GET_ACTIONS_AMOUNT), false)
                .params(Urls.USERNAME, MyApplication.mLogInDataBean.getUsername())
                .execute(calllBack);
    }

    /**
     * 获取运动统计底部数据
     */
    public void getFooterCount(HttpCallBack calllBack) {
        OkGo.get(Urls.URL_FOOTER_COUNT)
                .params(getCheckParams(Urls.PARAMS_GET_FOOTER_COUNT), false)
                .params(Urls.USERNAME, MyApplication.mLogInDataBean.getUsername())
                .execute(calllBack);
    }

    /**
     * 获取今日的运动情况
     */
    public void getTodayActionDetail(HttpCallBack calllBack) {
        OkGo.get(Urls.URL_GET_TODAYACTION_DETAIL)
                .params(getCheckParams(Urls.PARAMS_GET_ONE_ACTION_FINISHED_TODAY), false)
                .params(Urls.USERNAME, MyApplication.mLogInDataBean.getUsername())
                .execute(calllBack);
    }

    /**
     * 获取运动计划
     */
    public void getMyPlan(HttpCallBack callBack) {
        OkGo.get(Urls.URL_GET_USER_PLAN)
                .params(getCheckParams(Urls.PARAMS_GET_USER_PLAN), false)
                .params(Urls.USERNAME, MyApplication.mLogInDataBean.getUsername())
                .execute(callBack);
    }

    /**
     * 添加计数运动
     */
    public void addNumberAction(Map<String, String> map, HttpCallBack callBack) {
        OkGo.get(Urls.URL_ADD_NUMBER_ACTION)
                .params(map, false)
                .params(getCheckParams(Urls.PARAMS_ADD_NUMBER_ACTION), false)
                .params(Urls.USERNAME, MyApplication.mLogInDataBean.getUsername())
                .execute(callBack);
    }

    /**
     * 添加计时运动
     */
    public void addTimingAction(Map<String, String> map, HttpCallBack callBack) {
        OkGo.get(Urls.URL_ADD_TIMING_ACTION)
                .params(map, false)
                .params(getCheckParams(Urls.PARAMS_ADD_TIMING_ACTION), false)
                .params(Urls.USERNAME, MyApplication.mLogInDataBean.getUsername())
                .execute(callBack);
    }

    /**
     * 获取指定运动的记录
     *
     * @param actionCode
     */
    public void getSportRecord(int actionCode, int page, HttpCallBack callBack) {
        OkGo.get(Urls.URL_GET_SPORT_RECORD)
                .params(getCheckParams(Urls.PARAMS_GET_SPORT_RECORD), false)
                .params(Urls.USERNAME, MyApplication.mLogInDataBean.getUsername())
                .params("actionCode", actionCode)
                .params("page", page)
                .execute(callBack);


    }

    public void showErrorToast(Context context, int errorCode) {
        String errorMessage = "请检查当前网络!";
        switch (errorCode) {
            case -1000:
                errorMessage = "签名验证失败";
                break;
            case -1001:
                errorMessage = "短信验证码不正确";
                break;
            case -1002:
                errorMessage = "缺少参数";
                break;
            case -1004:
                errorMessage = "获取验证码失败";
                break;
            case -10001:
                errorMessage = "手机号码已注册";
                break;
            case -10002:
                errorMessage = "手机号码格式错误";
                break;
            case -10003:
                errorMessage = "注册失败";
                break;
            case -10004:
                errorMessage = "用户不存在";
                break;
            case -10005:
                errorMessage = "用户名或密码错误";
                break;
            case -10006:
                errorMessage = "用户异常";
                break;
            case -10007:
                errorMessage = "密码格式不正确";
                break;
            case -10008:
                errorMessage = "修改用户信息失败";
                break;
            default:
                break;

        }

        Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();

    }

}
